package hello

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/gobase/factory"
	"gitee.com/ymofen/gobase/gotask"
	"gitee.com/ymofen/golog"
	"gitee.com/ymofen/goplugs/pkg/base"
	"gitee.com/ymofen/supervalue"
	"time"
)

type PlgsHello struct {
	base.PlgBase
}

func (this *PlgsHello) OnCreate() {
	this.PlgBase.OnCreate()
	this.Log = golog.Default().WithOptionEx(golog.WithOptionPrefix(this.PlugId))
}

func (this *PlgsHello) ReloadConf(conf *wrapper.SuperValue) error {
	if conf == nil {
		return nil
	}
	this.Log.Infof("%s", conf.AsJSONString(false))

	gotask.DefaultPatrolTask().AddTask(this.PlugId, time.Second*5, func(id interface{}, args ...interface{}) {
		this.Log.Infof("test %s, hex:%s", gobase.NowIDString(), gobase.BufToHexStr(gobase.RandBuf(1024), 0, ""))
	})
	return nil
}

func (this *PlgsHello) Close() error {
	gotask.DefaultPatrolTask().DelTask(this.PlugId)
	this.PlgBase.ExecuteAllAndClose()
	this.PlgBase.Log = nil
	return nil
}

func CreatePlgsHelloFunc(args ...interface{}) (rval interface{}, err error) {
	obj := &PlgsHello{}
	if len(args) > 0 {
		if str, ok := args[0].(string); ok {
			obj.PlugId = str
		}
	}

	rval = obj
	return
}

func init() {
	factory.RegisterCreateFunc("plgs.helloworld", CreatePlgsHelloFunc)
}
